package com.haoxi.gateway.smgpmsg;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class SMGPLoginMessage extends SMGPMessage {

    private StringBuffer strBuf;

    public SMGPLoginMessage(String clientId, String shared_Secret,
            int loginMode, Date timestamp, int version)
            throws IllegalArgumentException {
        if (clientId == null)
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:clientId  value is null.");
        if (clientId.length() > 8)
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:length is larger than 8");
        if (loginMode < 0 || loginMode > 255)
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:loginMode less than 0 or larger than 255.");
        if (version < 0 || version > 255)
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:version less than 0 or larger than 255.");
        int len = 42;
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(SMGPCommon.SMGP_LOGIN, buf, 4);
        System.arraycopy(clientId.getBytes(), 0, buf, 12, clientId.length());
        if (shared_Secret != null)
            len = clientId.length() + 17 + shared_Secret.length();
        else
            len = clientId.length() + 17;
        byte tmpbuf[] = new byte[len];
        int tmploc = 0;
        System.arraycopy(clientId.getBytes(), 0, tmpbuf, 0, clientId.length());
        tmploc = clientId.length() + 7;
        if (shared_Secret != null) {
            System.arraycopy(shared_Secret.getBytes(), 0, tmpbuf, tmploc,
                    shared_Secret.length());
            tmploc += shared_Secret.length();
        }
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss");
        String tmptime = "0008080808";
        tmptime = format.format(new Date());
        System.arraycopy(tmptime.getBytes(), 0, tmpbuf, tmploc, 10);
        md5(tmpbuf, 0, len, buf, 20);
        buf[36] = (byte) loginMode;
        TypeConvert.int2byte(Integer.parseInt(tmptime), buf, 37);
        buf[41] = (byte) version;
        strBuf = new StringBuffer();
        strBuf.append(",clientId=").append(clientId);
        strBuf.append(",shared_Secret=").append(shared_Secret);
        strBuf.append(",loginMode=").append(loginMode);
        strBuf.append(",version=").append(version);
    }

    public String toString() {
        StringBuffer outStr = new StringBuffer();
        outStr.append("SMGPLoginMessage:");
        outStr.append("PacketLength=").append(buf.length);
        outStr.append(",RequestID=").append("0x").append(
                Integer.toHexString(getRequestId()));
        outStr.append(",Sequence_Id=").append(getSequenceId());
        if (strBuf != null)
            outStr.append(strBuf.toString());
        return outStr.toString();
    }

    public static void md5(byte data[], int offset, int length, byte digest[],
            int dOffset) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data, offset, length);
            md5.digest(digest, dOffset, 16);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (DigestException ex) {
            ex.printStackTrace();
        }
    }

    public int getRequestId() {
        return SMGPCommon.SMGP_LOGIN;
    }
}
